﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Reflection;
using System.Windows;
using System.Xml.Linq;
using LogLib;

namespace AddressKeyBoard
{
    public class UIConfig
    {
        private string _error = "";
        public string Error
        {
            get { return _error; }
        }

        private string _file = "";      
        private XDocument _xmlDoc = null;
        private XElement _root = null;

        public List<XButton> ListButtons = new List<XButton>();
        public List<XLabel> ListLabels = new List<XLabel>();
        public UIConfig(string file)
        {
            _file = file; 
        }

        public bool OnReading()
        {
            if (!OpenXmlDOC())
            {
                return false;
            }
            GetXmlObject<XButton>("Buttons", "Button", out ListButtons);
            GetXmlObject<XLabel>("Labels", "Label", out ListLabels);
            return true;
        }

        private bool OpenXmlDOC()
        {
            _error = "";
            try
            {
                _xmlDoc = XDocument.Load(_file);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                Log.WriteException(ex.ToString());
                return false;              
            }
            
            if (_xmlDoc == null)
            {
                _error = "加载配置文件失败.";
                Log.WriteAsync(_error);
                return false;
            }

            _root = _xmlDoc.Element("Window");
            if (_root == null)
            {
                _error = "找不到名为window的根节点.";
                Log.WriteAsync(_error);
                return false;
            }
            return true;
        }

        private void GetXmlObject<T>(string parentName, string nodeName, out List<T> listOuts) where T : new()
        {
            listOuts = new List<T>();
            XElement parent = _root.Element(parentName);
            if (parent == null)
                return;
            IEnumerable<XElement> elems = parent.Elements(nodeName);
            if (elems == null)
                return;
            if (elems.Count() == 0)
                return;
            string name = "", value = "";
            PropertyInfo propertyInfo = null;
            try
            {               
                Type type = typeof(T);              
                foreach (XElement e in elems)
                {
                    T temp = new T();
                    IEnumerable<XAttribute> atts = e.Attributes();
                    foreach (XAttribute att in atts)
                    {
                        name = att.Name.ToString();
                        value = att.Value.Trim();
                        propertyInfo = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
                        if (propertyInfo != null)
                        {
                            propertyInfo.SetValue(temp, value, null); //给对应属性赋值
                        }
                        else
                        {

                        }
                    }
                    listOuts.Add(temp);
                }
            }
            catch (System.Exception ex)
            {             
                MessageBox.Show(ex.Message);
                Log.WriteException(ex.ToString());
            }
        }
    }
}
